package com.zty.utils;

import com.zty.BO.FileChunkMeta;
import com.zty.errors.BusinessException;
import com.zty.errors.EnumClientException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author ZTY
 * @Description 策略模式
 * @Date 13:07 2023-12-25
 **/

@Component
public class ChunkAddressStrategy {

    private final Map<String,ChunkAddressBuilder> builderMap;

    public ChunkAddressStrategy(List<ChunkAddressBuilder> builderList){
        this.builderMap = new HashMap<>();
        builderList.forEach(bulder->builderMap.put(bulder.schema(),bulder));
    }

    public String get(FileChunkMeta fileChunkMeta){
        String schema = fileChunkMeta.getSchema();
        ChunkAddressBuilder addressBuilder = builderMap.get(schema);
        if (Objects.isNull(addressBuilder)){
            throw new BusinessException(EnumClientException.SCHEMA_DOES_NOT_SUPPORT);
        }
        return addressBuilder.build(fileChunkMeta);
    }
}
